package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private CategoryFeign categoryFeign;
    @Autowired
    private SkuFeign skuFeign;

    @Value("${pagepath}")
    private String pagePath;
    @Autowired
    private TemplateEngine templateEngine;

    @Override
    public void generateItemPage(String spuId) {

        //1.获取context对象，用于存储商品相关信息
        Context context = new Context();
        Map<String,Object> itemDate = this.getItemDate(spuId);
        context.setVariables(itemDate);
        //2.获取商品详情页面的存储位置
        File dir = new File(pagePath);
        //3.判断当前存储位置的文件夹是否存在，如果不存在，则新建
        if (!dir.exists()){
            dir.mkdir();
        }
        //4.定义输出流，完成文件的生成
        File file = new File(dir+"/"+spuId+".html");
        Writer out = null;
        try {
            out = new PrintWriter(file);
            //生成静态化页面
            /**
             * 1、模板名称
             * 2、context
             * 3、输出流
             */
            templateEngine.process("item",context,out);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //5.关闭流
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    public Map<String,Object> getItemDate(String spuId){
        Map<String,Object> resultData = new HashMap<>();

        //获取 spu
        Spu spu = spuFeign.findBySpuId(spuId).getData();
        resultData.put("spu",spu);
        //获取 图像信息
        if (spu != null ){
            String images = spu.getImages();
            if (StringUtils.isNotEmpty(images)){
                String[] imageList = images.split(",");
                resultData.put("imageList",imageList);
            }
        }
        //获取 分类信息
        Category category1 = categoryFeign.findById(spu.getCategory1Id()).getData();
        resultData.put("category1",category1);
        Category category2 = categoryFeign.findById(spu.getCategory2Id()).getData();
        resultData.put("category2",category2);
        Category category3 = categoryFeign.findById(spu.getCategory3Id()).getData();
        resultData.put("category3",category3);
        //获取 sku 信息
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        resultData.put("skuList",skuList);

        //获取 规格信息  ，将 商品规格信息 转换成 map 格式， 发送给 前台页面
        resultData.put("specificationList", JSON.parseObject(spu.getSpecItems(),Map.class));

        return resultData;
    }
}
